<template>
	<view class="u-p-20 paddingBottom">
		<!-- <scroll-view scroll-y scroll-with-animation class="u-tab-view menu-scroll-view"> -->
		<u-swipe-action>
			<u-swipe-action-item :show="show" :options="options" @click="action($event,item.id)" v-for="(item,index) in address"
				:key="index">
				<view class="swipe-action whiteBorder" @click="selectAddress(item.id)">
					<view class="swipe-action__content">
						<view class="top">
							<view class="name">{{ item.name }}</view>
							<view class="phone">{{ item.phone }}</view>
							<view class="tag" v-if="item.is_default">
								<text>默认</text>
							</view>
						</view>
						<view class="bottom">
							{{item.province+item.city+item.county+item.address}}
							<u-icon name="edit-pen" :size="22" color="#999999"
								@click.native.stop="modifyAddress(item.id)"></u-icon>
						</view>
					</view>
				</view>
			</u-swipe-action-item>
		</u-swipe-action>
		<!-- </scroll-view>
		<view>
			
		</view> -->
		<view class="addSite" @click="toAddSite">
			<view class="add">
				<u-icon name="plus" color="#ffffff" class="icon" :size="30"></u-icon>新建收货地址
			</view>
		</view>
	</view>
</template>
<script>
	let access_token, source = '';
	export default {
		data() {
			return {
				show: false,
				options: [{
					text: '设为默认',
					style: {
						backgroundColor: '#3c9cff'
					}
				}, {
					text: '删除',
					style: {
						backgroundColor: '#f56c6c'
					}
				}],
				address: []
			};
		},
		async onLoad() {
			if (!uni.$u.utils.isLogin()) {
				return;
			}
			//判断一下是从个人中心的收货地址来的还是从订单预览页面过来 
			if(getCurrentPages().length >1 ){
				source = getCurrentPages()[getCurrentPages().length - 2].route;
			}			
			access_token = uni.getStorageSync('access_token');
			let res = await uni.$u.http.get('/api/address', {
				header: {
					Authorization: 'bearer ' + access_token
				}
			});
			console.log(res);
			this.address = res.data;
		},
		methods: {
			toAddSite() {
				uni.$u.route('/pages/cart/newAddress');
			},
			async action(e, id) {
				if (e.index === 0) {
					//设为默认地址
					await uni.$u.http.post(`/api/address/${id}/default`, {
						_method: 'patch'
					}, {
						header: {
							Authorization: 'bearer ' + access_token
						}
					})
					uni.$u.toast('设置成功');
					this.show = false; //目前版本无效，等等软件升级
					if (source == 'pages/center/center') {
						return;
					}
					//设置好默认地址后，返回订单预览页面，地址自动更换成刚才设置的默认地址
					setTimeout(() => {
						uni.$u.route({
							url: "/pages/cart/order",
							params: {
								id: id
							}
						})
					}, 1000)
				} else {
					//删除
					await uni.$u.http.delete(`/api/address/${id}`, {}, {
						header: {
							Authorization: 'bearer ' + access_token
						}
					})
					uni.$u.toast('删除成功');
					if (source == 'pages/center/center') {
						return;
					}
					//删除后，停留在当前页面，可以继续操作其他地址，但已经删除的地址，也要在列表中删除。
					let tmp;
					tmp = this.address.filter((address) => { //过滤掉刚才删除的地址
						if (address.id != id) {
							return true; //返回真，就表示这一条数据满足要求，会存入tmp中。
						}
					})
					this.address = tmp;
				}
			},
			modifyAddress(id) {
				uni.$u.route({
					url: '/pages/cart/modifyAddress',
					params: {
						id: id
					}
				})
			},
			selectAddress(id) {
				if (source == 'pages/center/center') {
					return;
				}
				uni.$u.route({
					url: "/pages/cart/order",
					params: {
						id: id
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	/deep/ .u-swipe-action-item {
		overflow: scroll !important;
	}

	.paddingBottom {
		padding-bottom: 80px !important;
	}

	.top {
		display: flex;
		font-weight: bold;
		font-size: 34rpx;

		.phone {
			margin-left: 60rpx;
		}

		.tag {
			display: flex;
			font-weight: normal;
			align-items: center;

			text {
				display: block;
				width: 60rpx;
				height: 34rpx;
				line-height: 34rpx;
				color: #ffffff;
				font-size: 20rpx;
				border-radius: 6rpx;
				text-align: center;
				margin-left: 30rpx;
				background-color: rgb(49, 145, 253);
			}

			.red {
				background-color: red
			}
		}
	}

	.bottom {
		display: flex;
		margin-top: 20rpx;
		font-size: 28rpx;
		justify-content: space-between;
		color: #999999;
	}

	/deep/ .u-cell__title-text--large {
		font-size: 18px;
		font-weight: bold;
	}

	/deep/ .u-cell__label--large {
		font-size: 16px;
	}

	.swipe-action {
		&__content {
			padding: 25rpx 0;

			&__text {
				font-size: 18px;
				color: $u-main-color;
				padding-left: 30rpx;
			}
		}
	}

	.addSite {
		display: flex;
		justify-content: space-around;
		width: 600rpx;
		line-height: 100rpx;
		position: fixed;
		bottom: 30rpx;
		left: 80rpx;
		background-color: red;
		border-radius: 60rpx;
		font-size: 30rpx;
		z-index: 99999;

		.add {
			display: flex;
			align-items: center;
			color: #ffffff;

			.icon {
				margin-right: 10rpx;
			}
		}
	}
</style>
